package com.polaris.live.popup

import android.content.Context
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.MagicLampHistoryAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.PopupMagicLampHistoryBinding
import com.polaris.live.net.payerService
import com.polaris.live.translation.R.string
import com.polaris.live.widget.refresh.EmptyView

/**
 *  MagicLampHistoryPopup 神灯历史中奖
 * @author Created by 天晴 on 2024/11/25 19:18
 * @since 1.0.0
 **/
class MagicLampHistoryPopup(
    context: Context,
    val mViewModel: BaseViewModel,
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupMagicLampHistoryBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_magic_lamp_history
    }

    private val mAdapter: MagicLampHistoryAdapter by lazy {
        MagicLampHistoryAdapter()
    }
    private val emptyView by lazy { EmptyView(context) }
    override fun onCreate() {
        super.onCreate()
        getLampHistory()

        mBinding = PopupMagicLampHistoryBinding.bind(popupImplView)
        val lp = mBinding.bottom.layoutParams as ViewGroup.LayoutParams
        lp.height = NavigationBarUtils.getNavigationBarHeight(context)
        mBinding.bottom.layoutParams = lp
        mBinding.cancel.setOnSingleClickListener {
            dismiss()
        }
        emptyView.setEmptyTip(
            R.drawable.magic_lamp_empty,
            StringUtils.getString(string.no_hava_data),
            ColorUtils.getColor(R.color.white)
        )
        mBinding.recycler.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State,
                ) {
                    val position = parent.getChildAdapterPosition(view) // 当前 item 的位置
                    val itemCount = state.itemCount // 总的 item 数量

                    // 设置顶部间距
                    if (position == 0) {
                        outRect.top = 10.autoDp
                    }

                    // 设置底部间距
                    if (position == itemCount - 1) {
                        outRect.bottom = 10.autoDp
                    }

                    // 设置 item 之间的间距
                    outRect.left = 0
                    outRect.right = 0
                    if (position != itemCount - 1) {
                        outRect.bottom = 10.autoDp
                    }
                }
            })
            itemAnimator = null
        }
        mAdapter.setEmptyView(emptyView)
        mBinding.refreshView.setOnLoadMoreListener {
            ++pageNumber
            getLampHistory()
        }
        mBinding.refreshView.setOnRefreshListener {
            pageNumber = 0
            getLampHistory()
        }

    }


    val pageSize = 20  // 每页记录数
    var pageNumber: Int = 0
    private fun getLampHistory() {
        mViewModel.request({
            payerService.getLampHistory(pageNumber, pageSize)
        }, {
            if (pageNumber == 0) {
                mAdapter.data.clear()
                mAdapter.setList(it.list)
            } else {
                mAdapter.addData(it.list)
            }
            if (pageNumber == 0) {
                mBinding.refreshView.finishRefresh()
            } else {
                mBinding.refreshView.finishLoadMore(0)
            }
            if (it.hasMore) {
                mBinding.refreshView.setEnableLoadMore(true)
            } else {
                mBinding.refreshView.setEnableLoadMore(false)
            }
        }, {
            if (pageNumber == 0) {
                mBinding.refreshView.finishRefresh(false)
                mBinding.refreshView.setEnableLoadMore(false)
            } else {
                mBinding.refreshView.finishLoadMore(0, false, false)
            }
            if (mAdapter.data.isNotEmpty()) {
                ToastGravity.showGravity(it.errorMsg)
            }
        })
    }


    companion object {

        fun show(
            context: Context,
            mViewModel: BaseViewModel,
        ) {
            XPopup.Builder(context)
                .isViewMode(false)
                .hasShadowBg(true)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .enableDrag(false)
                .asCustom(MagicLampHistoryPopup(context, mViewModel))
                .show()
        }

    }
}